﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.InsideManage
{
    public partial class ProductSaleAdd : BasePage
    {
        public string picpath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Request.Params["type"] != null)
                {
                   lbltitle.Text = "产品销售添加";
                }


                DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup!=" + PubEnums.RoleGroup.客户.ToString("D")), ddl负责人, "ID", "RealName");
                //DropDownListHelper.InitDropList<tbSaleCustomerEntity>(new tbSaleCustomerEntity().GetAll(), ddl公司名称, "ID", "公司名称");
                //DropDownListHelper.InitDropList<tbUserEntity>(new tbSaleCustomerEntity().GetAll(), ddl公司名称, "ID", "公司名称");
                if (Request.Params["id"] != null)
                {
                    string sid = Request.Params["id"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);

                        //this.lblTotalPrice.Visible = true;
                        //this.lbl总价.Visible = true;
                        lbltitle.Text = "产品销售编辑";
                    }
                }


                if (Request.Params["vid"] != null)
                {
                    string sid = Request.Params["vid"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                        btnSave.Visible = false;
                    }
                }
            }
        }


        private void DataShow(int troubleId)
        {
            tbProductSaleEntity entity = new tbProductSaleEntity();
            entity = entity.GetById(troubleId);

            if (entity != null)
            {
                ViewState["Entity"] = entity;
                if (entity.CustomerID.ToString() != null)
                {
                    tbUserEntity user = new tbUserEntity();

                    user = user.FindEntity("Id=" + entity.CustomerID);

                    txtConpanyName.Text = user.CompanyName.ToString();
                    txt地址.Text = user.公司地址;
                    txt公司规模.Text = user.公司规模;
                    txt公司电话.Text = user.Phone;
                    txt传真.Text = user.FAX;
                    txt公司邮箱.Text = user.Email;
                    txt网站.Text = user.公司网站;
                    txt负责人A.Text = user.负责人A;
                    txt负责人B.Text = user.负责人B;
                    txt电话A.Text = user.负责人A电话;
                    txt电话B.Text = user.负责人B电话;
                    txt手机A.Text = user.负责人A手机;
                    txt手机B.Text = user.负责人B手机;
                    txt负责人AEmail.Text = user.负责人A邮箱;
                    txt负责人BEmail.Text = user.负责人B邮箱;
                }

                ddl服务项目.SelectedValue = entity.服务项目;
                txt产品名称.Text = entity.产品名称;
                txt编号.Text = entity.编号;
                //txt供应商.Text = entity.供货商.ToString();
                //txt品牌.Text = entity.品牌.ToString();
                //txt类别.Text = entity.类别.ToString();
                //txt型号.Text = entity.型号;
                txt单位.Text = entity.单位;
                txt规格.Text = entity.规格;
                txt数量.Text = entity.数量.ToString();
                txt单价.Text = entity.单价.ToString();
                txt具体详情.Text= entity.具体详情;
                txt跟进情况一.Text = entity.跟进信息1.ToString();
                txt跟进情况二.Text = entity.跟进信息2.ToString();
                txt跟进情况三.Text = entity.跟进信息3.ToString();
                txt跟进情况四.Text = entity.跟进信息4.ToString();
                txt跟进情况五.Text = entity.跟进信息5.ToString();

                if (entity.单价.HasValue && entity.数量.HasValue)
                {
                    decimal temp = entity.数量.Value * entity.单价.Value;
                    //lbl总价.Text = temp.ToString();
                    //lbl总价.Visible = true;
                }

                txt具体详情.Text = entity.具体详情;

                if (!string.IsNullOrEmpty(entity.AttachmentPath))
                {
                    picpath = "../UpLoadFiles/" + entity.AttachmentPath;
                    AattachmentPath.Visible = true;
                    //HyperLinkoldPic.NavigateUrl = "StructureDiagramView.aspx?picname=" + entity.picpath;
                }

                ddl负责人.SelectedValue = entity.负责人.ToString();

                if (entity.日期时间.HasValue)
                {
                    txt日期时间.Text = entity.日期时间.Value.ToShortDateString();
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductSaleList.aspx", true);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Entity.tbProductSaleEntity entity = new ITOMMS.Entity.tbProductSaleEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbProductSaleEntity;
                }
                else
                {
                    if (Request.Params["type"] != null)
                    {

                        if (Request.Params["type"].ToString() == "p")
                        {
                            entity.SaleType = "Product";//只销售产品 
                        }
                        if (Request.Params["type"].ToString() == "s")
                        {
                            entity.SaleType = "ITService";//只销售产品
                        }
                    }

                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }

                tbUserEntity user = new tbUserEntity();
                user.CompanyName = txtConpanyName.Text.Trim();
                user.公司地址 = txt地址.Text.Trim();
                user.公司规模 = txt公司规模.Text.Trim();
                user.Phone = txt公司电话.Text.Trim();
                user.FAX = txt传真.Text.Trim();
                user.Email = txt公司邮箱.Text.Trim();
                user.公司网站 = txt网站.Text.Trim();
                user.负责人A = txt负责人A.Text.Trim();
                user.负责人B = txt负责人B.Text.Trim();
                user.负责人A电话 = txt电话A.Text.Trim();
                user.负责人B电话 = txt电话B.Text.Trim();
                user.负责人A手机 = txt手机A.Text.Trim();
                user.负责人B手机 = txt手机B.Text.Trim();
                user.负责人B电话 = txt电话B.Text.Trim();
                user.负责人B电话 = txt电话B.Text.Trim();
                user.负责人A邮箱 = txt负责人AEmail.Text.Trim();
                user.负责人B邮箱 = txt负责人BEmail.Text.Trim();
                user.SaveOrUpdate();

                entity.CustomerID = user.ID;
                entity.服务项目 = ddl服务项目.SelectedValue;
                entity.产品名称 = txt产品名称.Text.Trim();
                entity.编号 = txt编号.Text.Trim();
                //entity.供货商 = txt供应商.Text.Trim();
                //entity.品牌 = txt品牌.Text.Trim();
                //entity.类别 = txt类别.Text.Trim();
                //entity.型号 = txt型号.Text.Trim();
                //entity.单位 = txt单位.Text.Trim();
                //entity.规格 = txt规格.Text.Trim();
                entity.具体详情 = this.txt具体详情.Text.Trim();
                entity.跟进信息1 = this.txt跟进情况一.Text.Trim();
                entity.跟进信息2 = this.txt跟进情况二.Text.Trim();
                entity.跟进信息3 = this.txt跟进情况三.Text.Trim();
                entity.跟进信息4 = this.txt跟进情况四.Text.Trim();
                entity.跟进信息5 = this.txt跟进情况五.Text.Trim();

                if (txt数量.Text.Trim().Length > 0)
                {
                    entity.数量 = int.Parse(txt数量.Text);
                }
                if (txt单价.Text.Trim().Length > 0)
                {
                    entity.单价 = decimal.Parse(txt单价.Text);
                }

                if (entity.单价.HasValue && entity.数量.HasValue)
                {
                    decimal temp = entity.数量.Value * entity.单价.Value;
                    entity.总价 = temp;
                }

                txt具体详情.Text = entity.具体详情;
                entity.负责人 = ddl负责人.SelectedValue;

                DateTime mytime = new DateTime();
                if (DateTime.TryParse(txt日期时间.Text.Trim(), out  mytime))
                {
                    entity.日期时间 = mytime;
                }
                if (FileUpload1.FileName != string.Empty && FileUpload1.HasFile)
                {
                    string imageFile = string.Empty;
                    imageFile = SaveImgToServer(FileUpload1);
                    entity.AttachmentPath = imageFile;
                    //newPreview.InnerHtml = "    <img  src='../UpLoadFiles/" + entity.XImageURL + "'/> ";
                }
                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                entity.SaveOrUpdate(entity);
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
        }

        private string SaveImgToServer(FileUpload imageUpload)
        {
            string rs = string.Empty;
            try
            {
                if (imageUpload.PostedFile.FileName == "")
                {
                    return rs;
                }

                string fileName = Guid.NewGuid().ToString();


                string filePath = imageUpload.PostedFile.FileName.ToLower();
                string fileEx = filePath.Substring(filePath.LastIndexOf(".") + 1);
                if (!System.IO.Directory.Exists(Server.MapPath(@"~/UpLoadFiles\")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(@"~/UpLoadFiles\"));
                }


                //大小 10MB
                string Samllserverpath1 = Server.MapPath(@"~/UpLoadFiles\") + fileName + "." + fileEx;
                if (imageUpload.PostedFile.InputStream.Length > 10000000)
                {
                    MessageboxInfo.MessageBox(this, MessageType.ERROR, "附件太大");
                    return "";
                }

                //保存大图

                imageUpload.PostedFile.SaveAs(Samllserverpath1);

                //lblImage.Text = "<img src='uploadpic/" + xfilename + "." + fileEx + "' width='100' height='80'/>";
                rs = fileName + "." + fileEx;


            }
            catch
            {
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "附件上传失败");
            }
            return rs;
        }

        //protected void ddl公司名称_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (ddl公司名称.SelectedValue.ToString() != "-1")
        //    {
        //        VwSaleCustomerEntity customer = new VwSaleCustomerEntity();

        //        customer = customer.FindEntity("Id=" + ddl公司名称.SelectedValue.ToString());

        //        txt地址.Text = customer.公司地址;
        //        txt公司规模.Text = customer.公司规模;
        //        txt公司电话.Text = customer.公司电话;
        //        txt传真.Text = customer.公司传真;
        //        txt公司邮箱.Text = customer.公司邮箱;
        //        txt网站.Text = customer.公司网站;
        //        txt负责人A.Text = customer.负责人A;
        //        txt负责人B.Text = customer.负责人B;
        //        txt电话A.Text = customer.负责人A电话;
        //        txt电话B.Text = customer.负责人B电话;
        //        txt手机A.Text = customer.负责人A手机;
        //        txt手机B.Text = customer.负责人B手机;
        //    }
        //}
    }
}
